package com.itheima.controller;
/**
 * 会员管理
 */

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/member")
public class MemberController {
    @Reference
    private MemberService memberService;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
            PageResult pageResult  = memberService.pageQuery(
                    queryPageBean.getCurrentPage(),
                    queryPageBean.getPageSize(),
                    queryPageBean.getQueryString());
        return pageResult;
    }

    //新增
    @RequestMapping("/addMember")
    public Result addMember(@RequestBody Member member,Integer managerId){
        try {
            memberService.addMember(member,managerId);
            //新增成功
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        }catch (Exception e){
            //新增失败
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }

    }

//    //查询全部健康管理师
//    @RequestMapping("/findManagerByRole")
//    public Result findManagerByRole(){
//        List<Map<String, Object>> options =memberService.findManagerByRole();
//        if(options != null && options.size() > 0){
//            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,options);
//        }
//        return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
//    }

    //删除
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            memberService.delete(id);
        }catch (RuntimeException e){
            return new Result(false,e.getMessage());
        }catch (Exception e){
            return new Result(false, MessageConstant.DELETE_MEMBER_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_MEMBER_SUCCESS);
    }

    //编辑查询当前会员信息
    @RequestMapping("/findById")
    public Result findById(Integer memberId){
        Member member = memberService.findById(memberId);
        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS,member);
    }
    //编辑当前会员信息
    @RequestMapping("/edit")
    public Result edit(@RequestBody Member member){
        try {
            memberService.edit(member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.EDIT_MEMBER_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_MEMBER_SUCCESS);
    }


    //导出Excel报表
    @RequestMapping("/exportBusinessReport")
    public Result exportBusinessReport(HttpServletRequest request, HttpServletResponse response){

        return null;

    }


}
